package model;



import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

import MyScreen.MainPage;

public class AuditionCanvas extends Canvas {

	Text text;
	ThanhLan thanhlan;
	MainPage main;
	int widscr,heiscr;
	Image imgbgr;
	Image imgdown;
	Image imgdownpr;
	Image imgscore;
	Image btsubp;
	Image btsub;
	Image igtmp;
	ImageAll imall;
	boolean start=false;
	boolean datang=false;
	boolean touchbtsub=false;
	int xscore=50,yscore=100;
	int numberarrow[]={1, 2, 2, 3, 3, 4 ,4, 4, 5, 5, 6, 6 ,7 ,8 ,9};
	int level=0;
	int imgw,imgh;
	int xtam=70,ytam=280;
	int xbtsub=150,ybtsub=240;
	int topleft=Graphics.TOP|Graphics.LEFT;
	int vt=0,maxvt=8,diem=0;
	int huong=4;//0 trai,1 len,2 phai,3 xuong
	DayMuiTen daymuiten;
	public AuditionCanvas(MainPage main)
	{
		this.main=main;
		setFullScreenMode(true);
		init();
	}
	public void init()
	{		
		widscr=getWidth();
		heiscr=getHeight();
		try {
			imgbgr = Image.createImage("/bgr.png");
			imgdown = Image.createImage("/down.png");
			imgscore = Image.createImage("/score.png");
			imall=new ImageAll(imgscore);
			imgdownpr = Image.createImage("/downpress.png");
			btsubp= Image.createImage("/btsubp.png");
			btsub=Image.createImage("/btsub.png");
			igtmp=btsub;
			imgw=imgdown.getWidth();
			imgh=imgdown.getHeight();
		
		} catch (Exception e) {

		}
		thanhlan=new ThanhLan(main);
		level=thanhlan.getSovong();
		daymuiten=new DayMuiTen();
		daymuiten.randomArrow(numberarrow[level]);
		text=new Text();
		
	}
	protected void paint(Graphics g) {
		
		g.setColor(0xffffff);
		g.fillRect(0, 0, widscr,heiscr);
		
		//ve nen
		g.drawImage(imgbgr, 0, 0, topleft);
		//ve phim enter
		g.drawImage(btsub, xbtsub,ybtsub, topleft);
		//ve day mui ten
        daymuiten.draw(g);
        
		//ve diem cua game
		if (touchbtsub)
		{
			imall.drawRegion(g,diem,0,xscore,yscore);
		}
		//ve phim huong
		
		if (huong==DayMuiTen.arrHuong[0]){
		g.drawImage(imgdownpr,xtam-65,ytam-20, topleft);}//left}
		else {g.drawImage(imgdown,xtam-65,ytam-20, topleft);};
		if (huong ==DayMuiTen.arrHuong[1]){
		g.drawRegion(imgdownpr, 0, 0, imgw,imgh,Sprite.TRANS_MIRROR_ROT270, xtam-20,ytam-70,topleft);//up
		}else {g.drawRegion(imgdown, 0, 0, imgw,imgh,Sprite.TRANS_MIRROR_ROT270, xtam-20,ytam-70,topleft);};
		if (huong==DayMuiTen.arrHuong[2]){
		g.drawRegion(imgdownpr, 0, 0, imgw,imgh,Sprite.TRANS_ROT180, xtam+20,ytam-20,topleft);//right}
		}else {g.drawRegion(imgdown, 0, 0, imgw,imgh,Sprite.TRANS_ROT180, xtam+20,ytam-20,topleft);};
		if (huong==DayMuiTen.arrHuong[3]){
		g.drawRegion(imgdownpr, 0, 0, imgw,imgh,Sprite.TRANS_MIRROR_ROT90, xtam-20,ytam+20,topleft);//down
		}else {g.drawRegion(imgdown, 0, 0, imgw,imgh,Sprite.TRANS_MIRROR_ROT90, xtam-20,ytam+20,topleft);}
		
		thanhlan.draw(g);
		//ve xau score:
				text.drawString("score",10,80, g);
				text.drawString("level",10,100, g);
		}

	protected void pointerDragged(int x, int y) {
		// TODO Auto-generated method stub
	}
	protected void pointerPressed(int x, int y) {
		// TODO Auto-generated method stub
		//neu cham nut submit
		
		if (thanhlan.chophepbam && x>xbtsub && x<xbtsub +btsubp.getWidth() &&y>ybtsub && y<ybtsub+btsub.getWidth())
		{
		yscore=100;
		touchbtsub=true;
		btsub=btsubp;
		
			if (vt==numberarrow[level]) 
			{
			diem=thanhlan.getScore();
			
			}
			else 
			{
			diem=thanhlan.getmiss();
			daymuiten.setNumArrow(0);
			}
		thanhlan.setChophepbam(false);
		repaint();
		}
		if (thanhlan.chophepbam)kiemtranutnhan(x, y);
	}
	public void kiemtranutnhan(int x,int y)
	{
		
		int wid=imgdown.getWidth();
		int hei=imgdown.getHeight();
		//xtam-65,ytam-20
		//up xtam-20,ytam-70
		huong=4;
		if (x>xtam-65 && x<xtam-65+wid && y>ytam-20 && y<ytam-20+hei)
		{
			huong=DayMuiTen.arrHuong[0];
		} else 
			if (x>xtam-20 && x<xtam-20+wid && y>ytam-70 && y<ytam-70+hei)
			{
				huong=DayMuiTen.arrHuong[1];
			}
			else 
				if (x>xtam+20 && x<xtam+20+wid && y>ytam-20 && y<ytam-20+hei)
				{
					huong=DayMuiTen.arrHuong[2];
				} else 
					if (x>xtam-20 && x<xtam-20+wid && y>ytam+20 && y<ytam+20+hei)
				{
						huong=DayMuiTen.arrHuong[3];
				} else return;
		try {
			System.out.println("vt= "+vt+" :huong="+huong+" :daymuiten="+daymuiten.getHuong(vt));
		if (vt<=numberarrow[level] && huong==daymuiten.getHuong(vt)){
			
			daymuiten.setIsPress(vt);
			vt++;
			
		}else
		{
			if (vt<=numberarrow[level])
			{
			daymuiten.pressFalse();
			vt=0;
			}
		}
		}
		catch(Exception e)
		{}
		
	}
	protected void pointerReleased(int x, int y) {
		
		
		//neu nha nut submit 
		if (x>xbtsub && x<xbtsub +btsubp.getWidth() &&y>ybtsub && y<ybtsub+btsub.getWidth())
		{
		btsub=igtmp;
		repaint();
		}
		huong=4;
		
	}
	public void update()
	{
		
		yscore-=3;
		if (yscore<=0) {touchbtsub=false; yscore=100;}
		//kiem tra bi miss hay khong
		

		//khi thanh lan chay het
		if (thanhlan.getDistance()<=16 && level<numberarrow.length-1)
		{
			// thi tang len 1 cap do neu da bam
			if (!datang){
			level++;
			thanhlan.setChophepbam(true);
			daymuiten=null;
			daymuiten=new DayMuiTen();
			daymuiten.randomArrow(numberarrow[level]);
			vt=0;}
			datang=true;
		}
		else
		{
			datang=false;
		}
		thanhlan.update();
		
		repaint();
	}
	public void ht(String s)
	{
		System.out.println(s);
	}
	

}

